package leetcode;

public class Q0169 {
    private int quickSort(int[] nums, int l, int r, int k) {
        if (l >= r) return nums[l];
        int x = nums[l + r >> 1];
        int i = l - 1;
        int j = r + 1;
        while (i < j) {
            while (nums[++i] < x) ;
            while (nums[--j] > x) ;
            if (i < j) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }
        }
        return k <= j ? quickSort(nums, l, j, k) : quickSort(nums, j + 1, r, k);
    }

    public int majorityElement(int[] nums) {
        return quickSort(nums, 0, nums.length - 1, nums.length / 2);
    }

    public static void main(String[] args) {
        Q0169 sl = new Q0169();
        int[] nums = {2, 2, 1, 1, 1, 2, 2};
        System.out.println(sl.majorityElement(nums));
    }
}
